home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / Vector.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  4.1 KB  |  129 lines  |  [TEXT/Moml]

  1. (* Vector.sml -- new basis *)
  2.  
  3. type 'a vector = 'a vector;
  4.  
  5. val maxLen = 4194303;           (* = 2^22-1, for 32-bit architectures *)
  6.  
  7. local
  8.     prim_val vector_ : int -> 'x -> 'a vector         = 2 "make_vect";
  9.     prim_val sub_    : 'a vector -> int -> 'a         = 2 "get_vect_item";
  10.     prim_val update_ : 'a vector -> int -> 'a -> unit = 3 "set_vect_item";
  11. in
  12.  
  13. prim_val length : 'a vector -> int                   = 1 "vect_length";
  14.  
  15. fun fromList (vs : 'a list) =
  16.   let val n = List.length vs
  17.       val a = if n > maxLen then raise Size else vector_ n () : 'a vector
  18.       fun init [] i = ()
  19.         | init (v::vs) i = (update_ a i v; init vs (i+1))
  20.   in (init vs 0; a) end;
  21.  
  22. fun tabulate(n, f : int -> 'a) =
  23.   if n < 0 orelse n > maxLen then raise Size else
  24.   let val a = vector_ n () : 'a vector
  25.       fun init i = if i >= n then () else (update_ a i (f i); init (i+1))
  26.   in (init 0; a) end;
  27.  
  28. fun sub(v, i) =
  29.     if i < 0 orelse i >= length v then raise Subscript
  30.     else sub_ v i;
  31.  
  32. fun extract (vec : 'a vector, i, sliceend) =
  33.     let val n = case sliceend of NONE => length vec - i | SOME n => n
  34.         val newvec = if i<0 orelse n<0 orelse i+n > length vec then
  35.                          raise Subscript
  36.                      else
  37.                          vector_ n () : 'a vector
  38.         fun copy j = if j<n then (update_ newvec j (sub_ vec (i+j)); 
  39.                                   copy (j+1))
  40.                      else ()
  41.     in copy 0; newvec end;
  42.  
  43. fun concat vecs =
  44.     let fun acc [] len       = len
  45.           | acc (v1::vr) len = acc vr (length v1 + len)
  46.         val len = acc vecs 0
  47.         val newvec = if len > maxLen then raise Size else vector_ len ()
  48.         fun copyall to []       = ()
  49.           | copyall to (v1::vr) =
  50.             let val len1 = length v1
  51.                 fun copy j =
  52.                     if j<len1 then
  53.                         (update_ newvec (to+j) (sub_ v1 j); copy (j+1))
  54.                     else
  55.                         ()
  56.             in copy 0; copyall (to+len1) vr end
  57.     in copyall 0 vecs; newvec end;
  58.  
  59.  
  60. fun foldl f e a = 
  61.     let val stop = length a
  62.         fun lr j res = if j < stop then lr (j+1) (f(sub_ a j, res))
  63.                        else res
  64.     in lr 0 e end
  65.  
  66. fun foldr f e a =
  67.     let fun rl j res = if j >= 0 then rl (j-1) (f(sub_ a j, res))
  68.                        else res
  69.     in rl (length a - 1) e end
  70.  
  71. fun app f a = 
  72.     let val stop = length a
  73.         fun lr j = if j < stop then (f(sub_ a j); lr (j+1))
  74.                    else ()
  75.     in lr 0 end
  76.  
  77. fun map (f : 'a -> 'b) (a : 'a vector) : 'b vector = 
  78.     let val stop = length a
  79.         val newvec = vector_ stop () 
  80.         fun lr j = if j < stop then (update_ newvec j (f(sub_ a j)); 
  81.                                      lr (j+1))
  82.                    else ()
  83.     in lr 0; newvec end
  84.  
  85. fun sliceend (a, i, NONE) = 
  86.         if i<0 orelse i>length a then raise Subscript
  87.         else length a
  88.   | sliceend (a, i, SOME n) = 
  89.         if i<0 orelse n<0 orelse i+n>length a then raise Subscript
  90.         else i+n;
  91.  
  92. fun foldli f e (slice as (a, i, _)) = 
  93.     let fun loop stop =
  94.             let fun lr j res = 
  95.                 if j < stop then lr (j+1) (f(j, sub_ a j, res))
  96.                 else res
  97.             in lr i e end
  98.     in loop (sliceend slice) end;
  99.  
  100. fun foldri f e (slice as (a, i, _)) = 
  101.     let fun loop start =
  102.             let fun rl j res = 
  103.                     if j >= i then rl (j-1) (f(j, sub_ a j, res))
  104.                     else res
  105.             in rl start e end;
  106.     in loop (sliceend slice - 1) end
  107.  
  108. fun appi f (slice as (a, i, _)) = 
  109.     let fun loop stop = 
  110.             let fun lr j = 
  111.                     if j < stop then (f(j, sub_ a j); lr (j+1)) 
  112.                     else ()
  113.             in lr i end
  114.     in loop (sliceend slice) end;
  115.  
  116. fun mapi (f : int * 'a -> 'b) (slice as (a : 'a vector, i, _)) : 'b vector = 
  117.     let val stop = sliceend slice
  118.         val newvec = vector_ (stop - i) () 
  119.         fun loop stop = 
  120.             let fun lr j = 
  121.                     if j < stop then 
  122.                         (update_ newvec (j-i) (f(j, sub_ a j)); 
  123.                          lr (j+1)) 
  124.                     else ()
  125.             in lr i end
  126.     in loop stop; newvec end;
  127.  
  128. end
  129.